package com.neulogistics.contrib.procuredcenter.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.Goods;
import com.neulogistics.contrib.procuredcenter.domain.model.valueobject.VendorInfo;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.GoodsPO;
import org.springframework.beans.BeanUtils;

public class GoodsEntityPOConverter {
    private static final int Rate = 100;

    public static GoodsPO entityToPO(Goods goods){
        if(goods == null)
            return null;

        GoodsPO goodsPO = new GoodsPO();
        // 复制普通属性
        BeanUtils.copyProperties(goods, goodsPO);
        // json对象转换
        if(goods.getVendorInfo() != null){
            goodsPO.setVendorInfo(JSONUtil.parseObj(goods.getVendorInfo()));
        }
        // 价格转换
        if(goods.getGoodsPrice() != null){
            goodsPO.setGoodsPrice((int) (goods.getGoodsPrice() * Rate));
        }
        if(goods.getGoodsCost() != null){
            goodsPO.setGoodsCost((int) (goods.getGoodsCost() * Rate));
        }

        return goodsPO;
    }

    public static Goods POToEntity(GoodsPO goodsPO){
        if(goodsPO == null)
            return null;

        Goods goods = new Goods();
        // 复制普通属性
        BeanUtils.copyProperties(goodsPO, goods);
        // json对象转换
        goods.setVendorInfo(JSONUtil.toBean(goodsPO.getVendorInfo(), VendorInfo.class));
        // 价格转换
        goods.setGoodsPrice(Double.valueOf(goodsPO.getGoodsPrice()) / Rate);
        goods.setGoodsCost(Double.valueOf(goodsPO.getGoodsCost()) / Rate);

        return goods;
    }
}
